package at.fhj.antesk.steamvaluecalc.view;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.border.EmptyBorder;
import javax.swing.border.LineBorder;
import javax.swing.border.TitledBorder;
import javax.swing.table.DefaultTableModel;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import at.fhj.antesk.steamvaluecalc.Game;
import at.fhj.antesk.steamvaluecalc.resources.Configs;

public class PriceHistoryWindow extends JFrame {

	private static final long serialVersionUID = -4638967471117348684L;
	private JPanel contentPane;
	private JTable table;
	private Game selectedGame;
	private static Logger logger = LoggerFactory.getLogger(PriceHistoryWindow.class);
	
	public PriceHistoryWindow(Game selectedGame){
		this();
		this.selectedGame = selectedGame;
		setTitle("Price History for " + selectedGame.getName());
	}
	
	public PriceHistoryWindow() {
		setIconImage(Toolkit.getDefaultToolkit().getImage(PriceHistoryWindow.class.getResource("/resources/spicon.gif")));
		setResizable(false);
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 575, 386);
		setLocationRelativeTo(null);
		setVisible(true);
		contentPane = new JPanel();
		contentPane.setBackground(Color.DARK_GRAY);
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JPanel selectedGamePanel = new JPanel() {
			private static final long serialVersionUID = -4801773857670059198L;

			@Override
			public void paint(Graphics g) {
				try {
					String iconUrl = "";
					if (null == selectedGame.getLogo()) {
						URL resource = this.getClass().getResource("/resources/game.jpg");
						iconUrl = resource.toExternalForm();
					} else {
						iconUrl = selectedGame.getLogo();
					}
					BufferedImage imageFromURL = ImageIO.read(new URL(iconUrl));
					g.drawImage(imageFromURL, 0, 0, null);
				} catch (MalformedURLException e) {
					logger.error("URL invalid", e);
				} catch (IOException e) {
					logger.debug("No picture found under the given URL: {} - Using default image.", selectedGame.getLogo());
					URL resource = this.getClass().getResource("/resources/game.jpg");
					String iconUrl = resource.toExternalForm();
					BufferedImage imageFromURL;
					try {
						imageFromURL = ImageIO.read(new URL(iconUrl));
						g.drawImage(imageFromURL, 0, 0, null);
					} catch (Exception e1) {
						logger.error("An error occured while the default image was drawn.", e1);
					}
				}
			}
			
		};
		selectedGamePanel.setToolTipText("Open the community page for this game");
		selectedGamePanel.setBounds(12, 13, 184, 69);
		contentPane.add(selectedGamePanel);
		
		
		table = new JTable();
		table.setModel(new DefaultTableModel(
			new Object[][] {
				{null, null, null, null, null},
			},
			Configs.getInstance().getCsvMap().get("price.history.table.headers")
		) {
			private static final long serialVersionUID = -2917542981727744857L;
			Class[] columnTypes = new Class[] {
				String.class, Object.class, Object.class, Object.class, Object.class
			};
			public Class getColumnClass(int columnIndex) {
				return columnTypes[columnIndex];
			}
		});
		table.setBounds(12, 95, 545, 250);

		JScrollPane scrollPane = new JScrollPane(table);
		scrollPane.setBackground(Color.DARK_GRAY);
		scrollPane.setBorder(new TitledBorder(new LineBorder(new Color(192, 192, 192)), "PRICE CHANGES", TitledBorder.LEADING, TitledBorder.TOP, null, new Color(255, 255, 255)));
		scrollPane.setBounds(12, 95, 545, 250);
		contentPane.add(scrollPane);
	}
	public JTable getTable() {
		return table;
	}
}
